package com.um.jdy.platform.shop.manager;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.um.jdy.common.shop.enums.DiyEnum;
import com.um.jdy.common.shop.service.DiyService;
import com.um.jdy.common.shop.entity.po.Diy;
import com.um.jdy.common.shop.entity.dto.DiyDTO;
import com.um.jdy.common.shop.entity.vo.DiyVO;
import com.um.jdy.platform.shop.entity.dto.DiyGetDTO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BasePlusManager;
import com.um.springboot.starter.orm.params.ListDTO;
import com.um.springboot.starter.orm.params.PageDTO;
import com.um.springboot.starter.service.CacheService;
import com.um.springboot.starter.service.CommonService;
import com.um.springboot.starter.utils.JSONUtil;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 管理类：页面diy表
 */
@Component
public class DiyManager extends BasePlusManager<DiyService,Diy,DiyDTO,DiyVO, PageDTO, ListDTO> {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    CommonService commonService;

    public DiyVO get(DiyGetDTO diyGetDTO) {
        Diy diy = service.getEntity(new String[]{"pro_name", "type"}, new Object[]{diyGetDTO.getProName(), diyGetDTO.getType()});
        DiyVO diyVO = UMBeanUtils.copyBean(diy, DiyVO.class);
        return diyVO;
    }

    public Response<Diy> addEntity(DiyDTO dto) {
        Diy entity = UMBeanUtils.copyBean(dto, Diy.class);
        entity = this.getSaveEntity(entity);
        entity.setCreateTime(DateUtil.date());
        Diy diy = this.service.addEntity(entity);
        return Response.success(diy);
    }

    public Response<Diy> updateEntity(String id, DiyDTO dto) {
        Diy entity = this.service.getEntity(id);
        BeanUtils.copyProperties(dto, entity);
        entity = this.getSaveEntity(entity);
        entity.setCreateTime(DateUtil.date());
        Diy diy = this.service.updateEntity(entity);
        //删除缓存
//        redisTemplate.delete(DiyEnum.Type.Home.name() + "-" + service.getComId());
        //删除缓存
        String redisTemplateKey = dto.getProName() + "-" + service.getComId();
        redisTemplate.delete(redisTemplateKey);
        return Response.success(diy);
    }

    public Diy getSaveEntity(Diy entity) {
        Diy diy = UMBeanUtils.copyBean(entity, Diy.class);
        Map pageData = JSONUtil.parseJSONToMap(diy.getProValue());
        List<Map> componentList = (List<Map>)pageData.get("items");

        for (Map component : componentList) {

            switch (StringUtil.getTrim(component.get("component_name"))){
                case "MakeupData":
                case "SellingData":
                    List<Map> data_items = JSONUtil.parseJSONToList(StringUtil.getTrim(component.get("data_items")));
                    for(int i=data_items.size()-1;i>=0;i--){
                        Map goods = data_items.get(i);
                        Map update_goods = commonService.queryEntity("shop_goods",goods.get("shop_goods_id"));

                        if(update_goods == null || update_goods.size() == 0){
                            data_items.remove(goods);
                        }else{

                            goods.put("thumb",JSONUtil.parseJSONToList(StringUtil.getTrim(update_goods.get("thumb"))));
                            goods.put("price",update_goods.get("price"));
                            goods.put("shop_goods_name",update_goods.get("shop_goods_name"));
                        }
                    }
                    component.put("data_items",data_items);
                    break;
                case "NewGiftData":
                case "GiftCertificatesData":
                    data_items = JSONUtil.parseJSONToList(StringUtil.getTrim(component.get("data_items")));
                    for(int i=data_items.size()-1;i>=0;i--){
                        Map coupons = data_items.get(i);
                        Map update_coupons = commonService.queryEntity("activity_coupons",coupons.get("activity_coupons_id"));
                        update_coupons.put("create_time", DateUtil.parse(StrUtil.toString(update_coupons.get("create_time"))).toString());
                        update_coupons.put("start_time",DateUtil.parse(StrUtil.toString(update_coupons.get("start_time"))).toString());
                        update_coupons.put("end_time",DateUtil.parse(StrUtil.toString(update_coupons.get("end_time"))).toString());
                        if(update_coupons == null || update_coupons.size() == 0){
                            data_items.remove(coupons);
                        }else{
                            data_items.set(i,update_coupons);
                        }
                    }
                    component.put("data_items",data_items);
                    break;
                case "GiftCouponsData":
                    Map data_item = JSONUtil.parseJSONToMap(StringUtil.getTrim(component.get("data_items")));
                    Map update_coupons = commonService.queryEntity("activity_coupons",data_item.get("activity_coupons_id"));
                    update_coupons.put("create_time",DateUtil.parse(StrUtil.toString(update_coupons.get("create_time"))).toString());
                    update_coupons.put("start_time",DateUtil.parse(StrUtil.toString(update_coupons.get("start_time"))).toString());
                    update_coupons.put("end_time",DateUtil.parse(StrUtil.toString(update_coupons.get("end_time"))).toString());
                    component.put("data_items",update_coupons);
                    break;
            }
        }

        pageData.put("items",componentList);
        diy.setProValue(JSONUtil.toJSONString(pageData));
        return diy;
    }
}
